package com.psa.processor;

import com.psa.element.IJson;
import com.psa.element.Interchange;
import com.psa.element.RequestCmd;
import com.psa.element.RequestMessage;
import com.psa.system.R;
import com.psa.tool.DBTool;
import com.psa.tool.IGeneralEventCallback;
import com.psa.tool.IHttpCallback;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.element.JsonObject;

/**
 *
 * @author david
 * @date 2014-11-18
 */
public class CustomerEditProcessor implements IHttpCallback {
    private String AddKey = "addcustomer";
    private String EditKey = "editcustomer";
    private IGeneralEventCallback generalEventCallback;

    public String getAddKey() {
        return AddKey;
    }

    public String getEditKey() {
        return EditKey;
    }

    public void setGeneralEventCallback(IGeneralEventCallback generalEventCallback) {
        this.generalEventCallback = generalEventCallback;
    }

    @Override
    public void callCompleted(RequestMessage message, IJson requestCmd) {
        JsonObject jsonObject = new JsonObject(((RequestCmd)requestCmd).Result);
        int id = 0;
        String msg = "";
        if (jsonObject != null){
            if (jsonObject.getProperty("ID")!=null) {
                id = jsonObject.getProperty("ID").getInteger();
            }
            if (jsonObject.getProperty("Msg")!=null) {
                msg = jsonObject.getProperty("Msg").getValue();
            }
        }
        if (id > 0){
            String[] pp = ((RequestCmd)message.Request).Param.split("\\|");
            int hid = Integer.parseInt(pp[0]);
            Interchange interchange = new Interchange();
            interchange.ID = id;
            interchange.Target = "Customer";
            interchange.Requirement = (message.MessageKey.equals(AddKey)?"A":"E");
            interchange.Status = "I";
            interchange.Contents = pp[1];
            interchange.Message = "";
            if (hid == id) {
                interchange.update(DBTool.getInstance());
            } else {
                interchange.insert(DBTool.getInstance());
            }
            SystemStatus.showMessage(SystemStatus.MainActivity, msg);
        }
    }

    @Override
    public void callError(RequestMessage message, String err) {
        SystemStatus.personOrderActivity = null;
        String CallbackMsg = SystemStatus.MainActivity.getString(R.string.app_error_msg)+ err;
        SystemStatus.showMessage(SystemStatus.MainActivity, CallbackMsg);
    }


}
